package app.framework.util;

import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 集中化管理本地缓存
 */
public class LocalCacheUtil {
	private static final Logger logger = LoggerFactory.getLogger(LocalCacheUtil.class);

	private static Map<String, LoadingCache> cachePool = Maps.newHashMap();

	public static LoadingCache getCache(String name){
	    return cachePool.get(name);
    }

	public static void addCache(String name, LoadingCache cache){
		cachePool.put(name,cache);
    }

	public static void invalidate(String cacheName, String cacheKey){
		LoadingCache cache = cachePool.get(cacheName);
		if(null == cache){
			logger.info("本地缓存[{}]不存在，忽略刷新！",cacheName);
			return;
		}
		cache.invalidate(cacheKey);
	}
}
